﻿using System;
using System.Data;

namespace Flagwind.Data
{
	/// <summary>
	/// 查询异常类。
	/// </summary>
	public class QueryException : Exception
	{
		private string _sql;

		/// <summary>
		/// SQL
		/// </summary>
		public string Sql
		{
			get
			{
				return _sql;
			}
			set
			{
				_sql = value;
			}
		}

		private CommandType _cmdType;

		/// <summary>
		/// 命令类型
		/// </summary>
		public CommandType CmdType
		{
			get
			{
				return _cmdType;
			}
			set
			{
				_cmdType = value;
			}
		}

		private IDataParameter[] _parameters;

		/// <summary>
		/// 参数
		/// </summary>
		public IDataParameter[] Parameters
		{
			get
			{
				return _parameters;
			}
			set
			{
				_parameters = value;
			}
		}

		private bool _inTransaction;

		/// <summary>
		/// 是否在事务中
		/// </summary>
		public bool InTransaction
		{
			get
			{
				return _inTransaction;
			}
			set
			{
				_inTransaction = value;
			}
		}

		private string _connectionString;

		/// <summary>
		/// 连接字符串
		/// </summary>
		public string ConnectionString
		{
			get
			{
				return _connectionString;
			}
			set
			{
				_connectionString = value;
			}
		}

		//private string _message;
		/// <summary>
		/// 默认构造函数
		/// </summary>
		public QueryException(string message) : base(message)
		{
		}

		/// <summary>
		/// 使用命令信息初始化构造函数
		/// </summary>
		/// <param name="message"></param>
		/// <param name="sql"></param>
		/// <param name="cmdType"></param>
		/// <param name="parameters"></param>
		/// <param name="inTransaction"></param>
		/// <param name="connectionString"></param>
		public QueryException(string message, string sql, CommandType cmdType, IDataParameter[] parameters, bool inTransaction, string connectionString) : base(message)
		{
			//_message = message;
			this.Sql = sql;
			this.CmdType = cmdType;
			this.Parameters = parameters;
			this.InTransaction = inTransaction;
			this.ConnectionString = connectionString;
		}

		/// <summary>
		/// 错误信息
		/// </summary>
		override public string Message
		{
			get
			{
				string paraString = "";
				if(this.Parameters != null)
				{
					foreach(IDataParameter p in this.Parameters)
					{
						if(p != null)
						{
							paraString += "Parameter[\"" + p.ParameterName + "\"]\t=\t\"" + Convert.ToString(p.Value) + "\"  \t\t\t//DbType=" + p.DbType.ToString() + "\r\n";
						}
					}
				}
				if(!string.IsNullOrEmpty(this.Sql))
				{
					return "查询错误：\r\nDataBase ErrorMessage:" + base.Message + "\r\n" + string.Format("SQL:{0}\r\nCommandType:{1}\r\nParameters:\r\n{2}", this.Sql, this.CmdType, paraString); // _message;
				}
				else
				{
					return "查询错误：\r\nDataBase ErrorMessage:" + base.Message + "\r\n"; // _message;
				}
			}
		}
	}
}